//  -------------------- iSharePoint -------------------- //
// 
//  Company:	IT Complex, LLC
//  Project:	Smart.Common
//  File name:	SqlQueryBuilderInItem.cs
//  Developer:	Solomatov Igor
//  Created:	15.04.2012
// 
//  -------------------- iSharePoint -------------------- //

using System.Linq;

namespace Smart.Common.Entities
{
	public class SqlQueryBuilderInItem<TExecutorType> : SqlQueryBuilderConditionItem<TExecutorType>
		where TExecutorType : class
	{
		/// <summary>
		/// Values
		/// </summary>
		public string[] Values { get; set; }

		public SqlQueryBuilderInItem(string field, params string[] values)
			: base(field, "IN", null)
		{
			Values = values;
		}

		/// <summary>
		/// Return Cache code
		/// </summary>
		public override int GetCacheCode()
		{
			return
				base.GetCacheCode() ^
				Values.Aggregate(0, (accu, value) => accu ^ value.GetHashCode());
		}

		/// <summary>
		/// Format QueryBuilder statement
		/// </summary>
		public override string FormatQuery()
		{
			var fieldName = (Query == null ? Field : Query.Meta.FieldName(Field));

			return string.Format("{1} {0} ({2})", Op, fieldName, Values.CastToString(", "));
		}
	}
}